/*
 * @Author: asushiye
 * @Date: 2020-07-07 09:41:13
 * @Commit: wechatkey, notice, message
 */

import { Injectable } from '@angular/core';
import { ActivatedRoute, NavigationExtras, Params, Router, UrlTree } from '@angular/router';
import { environment } from '@env/environment';

@Injectable({
  providedIn: 'root'
})
export class AfNavigationService {
  constructor(
    private router: Router,
    private actiateRoute: ActivatedRoute
  ) {}

  //打开url地址，可以是外部地址，也可以内部地址
  openUrl(url: any, newPage: Boolean) {
    if (newPage) {
      window.open(url);
    } else {
      window.location.href = url;
    }
  }

  // 判断是否是微信浏览器
  isWechatBrowser() {
    const ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i)) {
      return true;
    }
    return false;
  }

  // 路由功能
  navigateByUrl(url: string | UrlTree, extras?: NavigationExtras): Promise<boolean> {
    return this.router.navigateByUrl(url, extras);
  }
  navigate(commands: any[], extras?: NavigationExtras): Promise<boolean> {
    return this.router.navigate(commands, extras);
  }
  // 获取路由查询参数
  getQueryParam(): Params {
    return this.actiateRoute.snapshot.queryParams;
  }
  // 按资源获取不同环境下，请求地址  nginx服务器root地址(默认/)/api/resource
  buildUrl(resource: any) {
    return `${document.location.pathname}${environment.api.baseUrl.substring(2, 5)}${resource}`;
  }
}
